package backend.Instruction;

import backend.component.RISCVblock;
import backend.Register.RISCVoperand;

import java.util.ArrayList;
import java.util.List;

public class RISCVbeqz extends RISCVinst {
    private RISCVblock target;
    private List<RISCVoperand> operands = new ArrayList<>();

    public RISCVbeqz(List<RISCVoperand> operands,RISCVblock target) {
        this.operands = operands;
        this.target = target;
        setSrc(operands.get(0));
    }


    public RISCVoperand getSrc(){
        return operands.get(0);
    }

    public void setSrc(RISCVoperand src) {
        addUseReg(operands.get(0), src);
        operands.set(0, src);
    }

    public RISCVblock getTarget() {
        return target;
    }

    public void setTarget(RISCVblock target) {
        this.target = target;
    }

    public void replaceReg(RISCVoperand oldReg, RISCVoperand newReg) {
        //修改 src 寄存器的值
        if (operands.get(0) != null && operands.get(0).equals(oldReg)) {
            setSrc(newReg);
        }
    }
    @Override
    public String toString() {
        return "beqz " + operands.get(0) + ", " + target.getName() + "\n";
    }
}
